package org.blog.service;

import org.blog.dao.UserInfoDao;
import org.blog.helper.Result;
import org.blog.model.ActionType;
import org.blog.model.ModelPassport;
import org.blog.model.ModelUserInfo;
import org.blog.service.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;

public class UserInfoServiceImpl implements UserInfoService {

	@Autowired
	private UserInfoDao infoDao;

	@Override
	public Result<?> addUserInfo(ModelUserInfo t, ModelPassport passport) {
		if (ValidateHelper.validate(t, ActionType.ADD)) {
			t.setAction(1);
			long now = System.currentTimeMillis() / 1000;
			t.setActionTime(now);
			t.setCreateTime(now);
			t.setUsername(passport.getUsername());
			infoDao.addUserInfo(t);

			return new Result<Object>(200);
		} else {
			return new Result<Object>(400);
		}
	}

	@Override
	public Result<?> modifyUserInfo(ModelUserInfo t, ModelPassport passport) {
		if (ValidateHelper.validate(t, ActionType.MODIFY)) {
			t.setAction(2);
			t.setActionTime(System.currentTimeMillis() / 1000);
			t.setUsername(passport.getUsername());
			infoDao.modifyUserInfo(t);
			return new Result<Object>(200);
		} else {
			return new Result<Object>(400);
		}
	}

	@Override
	public Result<ModelUserInfo> getUserInfo(ModelPassport passport) {
		ModelUserInfo info = infoDao.getUserInfo(passport.getUsername());
		return new Result<ModelUserInfo>(info, 200);
	}

}
